package cn.kxlove.zhbj.pager.impl;

import android.app.Activity;
import android.view.View;
import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.kxlove.zhbj.MainActivity;
import cn.kxlove.zhbj.domain.respone.NewsMenuBean;
import cn.kxlove.zhbj.fragment.LeftMenuFragment;
import cn.kxlove.zhbj.pager.BaseMenuDetailPager;
import cn.kxlove.zhbj.pager.BasePager;
import cn.kxlove.zhbj.pager.impl.menu.InteractMenuDetailPager;
import cn.kxlove.zhbj.pager.impl.menu.NewsMenuDetailPager;
import cn.kxlove.zhbj.pager.impl.menu.PhotoMenuDetailPager;
import cn.kxlove.zhbj.pager.impl.menu.TopicMenuDetailPager;
import cn.kxlove.zhbj.common.Common;

/**
 * @author kxlove 16/1/27 20:10
 */
public class NewsCenterPager extends BasePager {

    private List<BaseMenuDetailPager> mMenuDetailPagers;// 菜单详情页集合
    private NewsMenuBean mNewsMenuBean;// 网络返回数据

    public NewsCenterPager(Activity activity) {
        super(activity);
    }

    @Override
    public void initData() {

        getDataFromServer();
    }

    private void getDataFromServer() {
        RequestParams params = new RequestParams(Common.CATEGORY_URL);
        //缓存时间设置
        params.setCacheMaxAge(1000 * 60);
        x.http().get(params, new Callback.CacheCallback<NewsMenuBean>() {

            @Override
            public boolean onCache(NewsMenuBean result) {
                processData(result);
                return false;
            }

            @Override
            public void onSuccess(NewsMenuBean result) {
                processData(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void processData(NewsMenuBean result) {
        mNewsMenuBean = result;
        // 将数据传递给侧边栏
        // 1. 先获取MainActivity
        // 2. 通过MainActivity获取LeftMenuFragment
        // 3. 将数据设置给LeftMenuFragment
        MainActivity mainUI = (MainActivity) mActivity;
        LeftMenuFragment leftMenuFragment = mainUI.getLeftMenuFragment();
        leftMenuFragment.setmMenuData(mNewsMenuBean.data);

        mMenuDetailPagers = new ArrayList<>();
        mMenuDetailPagers.add(new NewsMenuDetailPager(mActivity,mNewsMenuBean.data.get(0).children));
        mMenuDetailPagers.add(new TopicMenuDetailPager(mActivity));
        mMenuDetailPagers.add(new PhotoMenuDetailPager(mActivity));
        mMenuDetailPagers.add(new InteractMenuDetailPager(mActivity));

        setCurrentMenuDetailPager(0);
    }

    /**
     * 设置当前菜单详情页
     *
     * @param position 定位
     */
    public void setCurrentMenuDetailPager(int position) {
        BaseMenuDetailPager pager = mMenuDetailPagers.get(position);// 获取当前被选中的菜单详情页
        View view = pager.mRootView;
        flContent.removeAllViews();// 清除之前所有的布局
        flContent.addView(view);// 给新闻中心的帧布局添加菜单详情页布局

        // 初始化菜单详情页数据
        pager.initData();

        // 更新页面标题
        String title = mNewsMenuBean.data.get(position).title;
        tvTitle.setText(title);
    }
}
